package com.rj2002.ctrl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rj2002.dao.GoodsMapper;
import com.rj2002.dao.GoodstypeMapper;
import com.rj2002.dao.LunboMapper;
import com.rj2002.dao.OrdersMapper;
import com.rj2002.dao.UserMapper;
import com.rj2002.pojo.Goods;
import com.rj2002.pojo.Goodstype;
import com.rj2002.pojo.Lunbo;
import com.rj2002.pojo.Orders;
import com.rj2002.pojo.User;
import com.rj2002.util.FileUploadUtils;
import com.rj2002.util.ResultUtil;

@Controller
@RequestMapping("/admin")
public class AdminDataCtrl {
	/*...*/
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private GoodstypeMapper typeMapper;
	
	@Autowired
	private GoodsMapper goodsMapper;

	@Autowired
	private LunboMapper lunbotu;
	
	@Autowired
	private OrdersMapper ordersMapper;

	@RequestMapping("/getLunBoTu")
	@ResponseBody
	public ResultUtil getLunBoTu(Integer page, Integer limit, HttpServletRequest request) {
		// 后端分页内容
		PageHelper.startPage(page, limit);
		PageInfo<Lunbo> pageInfo = new PageInfo<Lunbo>(lunbotu.selectAll());
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}
	
	@RequestMapping("/getGoodsListData")
	@ResponseBody
	public ResultUtil getGoodsListData(Integer page, Integer limit,String goodsname, HttpServletRequest request) {
		// 后端分页内容
		PageHelper.startPage(page, limit);
		PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsMapper.selectAll(goodsname));
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}
	
	@RequestMapping("/getOrders")
	@ResponseBody
	public ResultUtil getOrders(Integer page, Integer limit,Orders orders, HttpServletRequest request) {
		// 后端分页内容
		PageHelper.startPage(page, limit);
		PageInfo<Orders> pageInfo = new PageInfo<Orders>(ordersMapper.selectAll(orders));
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}
	
	@RequestMapping("/updateGoodsFlag")
	@ResponseBody
	public ResultUtil updateGoodsFlag(Goods goods, HttpServletRequest request) {
		ResultUtil resultUtil = new ResultUtil();
		try {
			goodsMapper.updateByPrimaryKeySelective(goods);
		} catch (Exception e) {
			resultUtil.setData("更新失败");
			resultUtil.setCode(500);
			return resultUtil;
		}
		resultUtil.setData("更新成功");
		resultUtil.setCode(0);
		return resultUtil;
	}
	
	@RequestMapping("/deleteTypeById")
	@ResponseBody
	public ResultUtil deleteTypeById(HttpServletRequest request, int id) {
		ResultUtil resultUtil = new ResultUtil();
		try {
			typeMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			resultUtil.setData("删除失败");
			resultUtil.setCode(500);
			return resultUtil;
		}
		resultUtil.setData("删除成功");
		resultUtil.setCode(0);
		return resultUtil;
	}
	
	@RequestMapping("/deleteGoodsById")
	@ResponseBody
	public ResultUtil deleteGoodsById(int id, HttpServletRequest request) {
		ResultUtil resultUtil = new ResultUtil();
		try {
			goodsMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			resultUtil.setData("删除失败");
			resultUtil.setCode(500);
			return resultUtil;
		}
		resultUtil.setData("删除成功");
		resultUtil.setCode(0);
		return resultUtil;
	}

	@RequestMapping("/lunbo/editLunbo")
	@ResponseBody
	public ResultUtil editLunbo(int id, MultipartFile file, HttpServletRequest request) {
		// 拿到之前的轮播图
		Lunbo lunbo = lunbotu.selectByPrimaryKey(id);
		// 将新的图片文件存储到本地 返回url地址再去更新数据库
		try {
			String url = FileUploadUtils.upload(file, "lunbo");
			String old = lunbo.getUrl();
			lunbo.setUrl(url);
			lunbotu.updateByPrimaryKey(lunbo);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(1l);
		resultUtil.setData(null);
		return resultUtil;
	}
	
	@RequestMapping("/LunboAdd")
	@ResponseBody
	public ResultUtil LunboAdd(MultipartFile file, HttpServletRequest request) {
		// 将新的图片文件存储到本地 返回url地址再去更新数据库
		try {
			String url = FileUploadUtils.upload(file, "lunbo");
			Lunbo lunbo = new Lunbo();
			lunbo.setUrl(url);
			lunbotu.insertSelective(lunbo);
		} catch (IOException e) {
			e.printStackTrace();
		}
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(1l);
		resultUtil.setData("新增成功");
		return resultUtil;
	}

	@RequestMapping("/deleteLunbo")
	@ResponseBody
	public ResultUtil deleteLunbo(int id, HttpServletRequest request) {
		// 拿到之前的轮播图
		lunbotu.deleteByPrimaryKey(id);
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(1l);
		resultUtil.setData("ok");
		return resultUtil;
	}

	@RequestMapping("/getParentType")
	@ResponseBody
	public ResultUtil getParentType(HttpServletRequest request) {
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setData(typeMapper.getParentType());
		resultUtil.setCode(0);
		return resultUtil;
	}

	@RequestMapping("/addGoodType")
	@ResponseBody
	public ResultUtil addGoodType(Goodstype goodstype, HttpServletRequest request) {
		ResultUtil resultUtil = new ResultUtil();
		System.out.println(goodstype);
		if (goodstype.getTypelevel() == 0) {
			goodstype.setParentid(null);
		}
		goodstype.setFlag(1);
		// 对数据进行写入到数据库
		typeMapper.insertSelective(goodstype);
		resultUtil.setData("添加成功");
		resultUtil.setCode(0);
		return resultUtil;
	}

	@RequestMapping("/getTypeList")
	@ResponseBody
	public ResultUtil getTypeList(Integer page, Integer limit, Goodstype type, HttpServletRequest request) {
		// 后端分页内容
		PageHelper.startPage(page, limit);
		PageInfo<Goodstype> pageInfo = new PageInfo<Goodstype>(typeMapper.getTypeList(type));
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setData(pageInfo.getList());
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}

	@RequestMapping("/updateTypeFlag")
	@ResponseBody
	public ResultUtil updateTypeFlag(Goodstype tGoodstype, HttpServletRequest request) {
		// System.out.println(tGoodstype);
		ResultUtil resultUtil = new ResultUtil();
		try {
			typeMapper.updateFlag(tGoodstype);
		} catch (Exception e) {
			// TODO: handle exception
			resultUtil.setData("更新失败");
			resultUtil.setCode(500);
			return resultUtil;
		}
		resultUtil.setData("更新成功");
		resultUtil.setCode(0);
		return resultUtil;
	}

	@RequestMapping("/editTypename")
	@ResponseBody
	public ResultUtil editTypename(Goodstype tGoodstype, HttpServletRequest request) {
		// System.out.println(tGoodstype);
		ResultUtil resultUtil = new ResultUtil();
		try {
			typeMapper.editTypename(tGoodstype);
		} catch (Exception e) {
			// TODO: handle exception
			resultUtil.setData("更新失败");
			resultUtil.setCode(500);
			return resultUtil;
		}
		resultUtil.setData("更新成功");
		resultUtil.setCode(0);
		return resultUtil;
	}

	@RequestMapping("/userlist")
	@ResponseBody
	public List<User> userlist(String name, HttpServletRequest request) {
		return userMapper.userlist(name);
	}

	@RequestMapping("/userlist2")
	@ResponseBody
	public ResultUtil userlist2(Integer page, Integer limit, String name, HttpServletRequest request) {
		// 后端分页内容
		PageHelper.startPage(page, limit);
		PageInfo<User> pageInfo = new PageInfo<User>(userMapper.userlist(name));
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}

	@RequestMapping("/updateUser")
	@ResponseBody
	public ResultUtil updateUser(User user, HttpServletRequest request) {
		// 后端分页内容
		// System.err.println(user);
		ResultUtil resultUtil = new ResultUtil();
		userMapper.updateByPrimaryKeySelective(user);
		resultUtil.setCode(0);
		resultUtil.setData("ok");
		return resultUtil;
	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public ResultUtil deleteUser(int id, HttpServletRequest request) {
		// 后端分页内容
		// System.err.println(user);
		ResultUtil resultUtil = new ResultUtil();
		userMapper.deleteByPrimaryKey(id);
		resultUtil.setCode(0);
		return resultUtil;
	}

	@RequestMapping("/changePhone")
	@ResponseBody
	public ResultUtil changePhone(User user, HttpServletRequest request) {
		user.setId(((User) request.getSession().getAttribute("account")).getId());
		userMapper.changePhone(user);
		// 后端分页内容
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setData("修改成功");
		return resultUtil;
	}

	@RequestMapping("/changePwd")
	@ResponseBody
	public ResultUtil changePwd(String oldpwd, String newpwd, HttpServletRequest request) {
		ResultUtil resultUtil = new ResultUtil();
		User user = (User) request.getSession().getAttribute("account");
		User old = userMapper.selectByPwdAndId(oldpwd, user.getId());
		if (old != null && old.getAccount() != "") {
			// 旧密码与该账号信息能够匹配 就可以去修改密码
			user.setPasswords(newpwd);
			userMapper.changePwd(user);
		} else {
			resultUtil.setCode(0);
			resultUtil.setData("修改失败");
			return resultUtil;
		}
		// 后端分页内容
		resultUtil.setCode(0);
		resultUtil.setData("修改成功");
		return resultUtil;
	}

	@RequestMapping("/addUser")
	@ResponseBody
	public String addUser(User user, HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		int i = userMapper.addUser(user);
		if (i == 1) {
			return "添加成功!";
		} else {
			return "添加失败!";
		}
	}

	@RequestMapping("/changFlag")
	@ResponseBody
	public List<User> changFlag(User user, HttpServletRequest request) {
		userMapper.changFlag(user);
		// 对数据修改
		return userMapper.userlist(null);
	}
}
